package com.jsb.laundry.controller;

import com.jsb.laundry.common.exception.BaseResponse;
import com.jsb.laundry.common.exception.BusinessException;
import com.jsb.laundry.common.exception.ErrorCode;
import com.jsb.laundry.common.exception.ResultUtils;
import com.jsb.laundry.model.dto.posts.PostCommentLikeAddRequest;
import com.jsb.laundry.model.dto.posts.PostLikeAddRequest;
import com.jsb.laundry.service.JsbPostCommentsLikesService;
import com.jsb.laundry.service.JsbPostLikesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 *  评论点赞接口
 */
@RestController
@RequestMapping("/posts/comment/like")
@Api(value = "帖子相关接口",tags = "帖子")
@Slf4j
public class PostCommentLikeController {

    @Resource
    private JsbPostCommentsLikesService postCommentLikeService;

    /**
     * 点赞 / 取消点赞
     *
     * @param postCommentLikeAddRequest
     * @param request
     * @return resultNum 本次点赞变化数
     */
    @PostMapping("/op")
    @ApiOperation("帖子评论 点赞 / 取消点赞")
    public BaseResponse<Integer> doLike(@RequestBody PostCommentLikeAddRequest postCommentLikeAddRequest,
                                        HttpServletRequest request) {
        if (postCommentLikeAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        int commentId = postCommentLikeAddRequest.getCommentId();
        String userId = postCommentLikeAddRequest.getUserId();
        if (commentId == 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "评论ID 不能为空");
        }
        if ("".equals(userId)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "用户ID 不能为空");
        }

        int result = postCommentLikeService.doPostCommentLike(commentId, userId);
        return ResultUtils.success(result);
    }

}
